<?php
/**
* $Id: tv_thumb_helper.php 3233 2012-02-20 09:14:28Z nam.le $
* get Image thumbnail
* 
* note: onlyuse this script on upload file
* 
* @param mixed $filename
* @param mixed $width
* @param mixed $height
* @param mixed $r background red value
* @param mixed $g background green value
* @param mixed $b background blue value
* @return mixed
* 
* requirement asido 3rdparty library and imagersizer library
*/
if (!function_exists('getImage'))
{                                                              

    function getImage($filename = '', $width = 100, $height = 100, $r = 255, $g = 255, $b = 255, $crop = 0)
    {
        if (! file_exists($filename)) return NULL;

        $ci = & get_instance();
        $ci->load->library('ImageResizer');
        if (!ImageResizer::isImage($filename)) return NULL;
        
        
        $fileInfo = buildCacheInfo($filename, $width, $height, $r, $g, $b);
        //check cache
        if(file_exists($fileInfo['cache_filepath']))
        {
            return $fileInfo['cache_filepath'];
        }
        
        //Do not need to check origin file b/c we check it on photoDisplay()
        
        //check cache dir exist , if not -> create new one
        if(!is_dir($fileInfo['cache_dirpath']))
        {
            mkdir ($fileInfo['cache_dirpath'], 0777);
        }
        //make sure cache folder is writable
        if(!is_writable($fileInfo['cache_dirpath']))
        {
            chmod ($fileInfo['cache_dirpath'],0777);
        }

        //generate thumb
        if(!$crop)
        {
            $ci->imageresizer->thumb($fileInfo['origin_filepath'], $fileInfo['cache_filepath'], $width, $height, $r, $g, $b);
        }
        else
        {
            //Crop center img
            $ci->imageresizer->crop($fileInfo['origin_filepath'], $fileInfo['cache_filepath'], $width, $height);
        }

        return $fileInfo['cache_filepath'];
    }
}
 
/**
* generate cache file info
* 
* @param mixed $filename
* @param mixed $width
* @param mixed $height
* @param mixed $r background red value
* @param mixed $g background green value
* @param mixed $b background blue value
* @return mixed
*/
if (!function_exists('buildCacheInfo'))
{
    function buildCacheInfo($filename = '', $width = 0, $height = 0, $r = 255, $g = 255, $b = 255)
    {
        //dir setup
        //$root = '.';
        //$origindir = 'uploadedfiles';
        $cachedir = 'cachedimages';

        //afterfix string (important for making wildcard to remove file)
        $afterfix = 'qscache';
        $background = $r . '-' . $g . '-' . $b;

        //fileInfo
        $fileInfo = array ();
        $fileInfo = pathinfo($filename);
        
        // orgin dir
        $fileInfo['origin_dirpath'] = $fileInfo['dirname'];

        //cache dir
        $fileInfo['cache_dirpath'] = $fileInfo['origin_dirpath'] . '/' . $cachedir;

        //origin file path
        $fileInfo['origin_filepath'] = $filename;

        //generate cache info
        //$fileInfo['cache_wildcard'] = $fileInfo['filename'] . '_' . $afterfix;
        //$fileInfo['filename'] is not supported in php 5.1x so it is fixed as following
        $fileInfo['cache_wildcard'] = basename($filename, '.' . $fileInfo['extension']) . '_' . $afterfix;
        $fileInfo['cache_filename'] = $fileInfo['cache_wildcard'] . '_' . $width . '_' . $height .'_' . $background . '.' . $fileInfo['extension'];
        $fileInfo['cache_filepath'] = $fileInfo['cache_dirpath'] . '/' . $fileInfo['cache_filename'];
        
        return $fileInfo;
    }
}

/**
* remove thumb file was generated by getImage
*/
if (!function_exists('removeCache'))
{
    function removeCache($filename)
    {  
        $fileInfo = buildCacheInfo($filename);
        //setup remove
        $path = $fileInfo['cache_dirpath'] . '/';
        $match = $fileInfo['cache_wildcard'] . '*';
        //remove orginal image
        if(is_file($filename))
        {
            unlink($filename);
        }
        //start find and remove cached image
        static $delete_count = 0, $delete_size = 0;
        $files = glob($path.$match);
        foreach($files as $file)
        {
            if(is_file($file))
            {
                 $delete_size += filesize($file);
                 unlink($file);
                 $delete_count++;
            }
        }

        return "{$delete_count} files deleted with a total size of {$delete_size} bytes";
    }
}

if (!function_exists('removeFile'))
{
    function removeFile($fileName, $parentfolder)
    {   
        if(is_file($parentfolder.'/'.$fileName))
            unlink($parentfolder.'/'.$fileName);   
    }
}

if (!function_exists('getThumb'))
{
    function getThumb($fileName, $width = 0, $height = 0, $zoomcrop = 1, $filter = "")
    {   
        $url = "flexithumb.php?src={$fileName}&w={$width}&h={$height}&zc={$zoomcrop}&f={$filter}";
        return site_url($url);
    }
}
?>
